﻿using RndMaterialDisposalService.Attrbutes;
using RndMaterialDisposalService.Models.Entitys;
using RndMaterialDisposalService.Models.RequestModel;
using RndMaterialDisposalService.Repository;
using RndMaterialDisposalService.Services.Interface.VehicleDispatch;
using Microsoft.AspNetCore.Mvc;
using VertivWorkflowCommonService;

namespace RndMaterialDisposalService.Services.VehicleDispatchService
{
    /// <summary>
    /// 
    /// </summary>
    [NAutowired]
    public class CarConfigurationService : ICarConfigurationService
    {

        private readonly ILogger<CarConfigurationService> logger;
        private readonly AssetsManagementContext dbContext;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="assetsManagementContext"></param>
        public CarConfigurationService(ILogger<CarConfigurationService> logger, AssetsManagementContext assetsManagementContext)
        {
            this.logger = logger;
            dbContext = assetsManagementContext;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="carConfiguration"></param>
        /// <returns></returns>
        public async Task<int> AddCarConfiguration(CarConfigurationDto carConfiguration)
        {
            carConfiguration.SysID = Guid.NewGuid();
            await dbContext.CarConfiguration.AddAsync(carConfiguration);
            return await dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sysId"></param>
        /// <returns></returns>
        public async Task<int> DeleteCarConfiguration(Guid sysId)
        {
            var model = await dbContext.CarConfiguration.FirstOrDefaultAsync(p => p.SysID == sysId);
            dbContext.CarConfiguration.Remove(model);
            return await dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="carManageReq"></param>
        /// <returns></returns>
        public async Task<(List<CarConfigurationDto> list, int count)> GetCarConfigurationList(CarManagementReq carManageReq)
        {
            var data = dbContext.CarConfiguration.Where(p => 1 == 1);
            if (!string.IsNullOrEmpty(carManageReq.CarNumber)) {
                data = data.Where(p => !string.IsNullOrEmpty(p.CarNumber) && p.CarNumber.Contains(carManageReq.CarNumber));
            }
            if (!string.IsNullOrEmpty(carManageReq.DriverId))
            {
                data = data.Where(p => p.DriverId == carManageReq.DriverId);
            }
            if (!string.IsNullOrEmpty(carManageReq.CarType))
            {
                data = data.Where(p => !string.IsNullOrEmpty(p.CarType) && p.CarType.Contains(carManageReq.CarType));
            }
            var count = await data.CountAsync();
            var list = await data.Skip((carManageReq.PageIndex - 1) * carManageReq.PageSize).Take(carManageReq.PageSize).ToListAsync();
            
            return (list, count);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public async Task<(List<CarConfigurationDto> list, int count)> GetCarListByKeyword(string keyword, int PageIndex, int PageSize)
        {
            var data = dbContext.CarConfiguration.Where(p => 1 == 1);
            if (!string.IsNullOrEmpty(keyword))
            {
                data = data.Where(p => (!string.IsNullOrEmpty(p.CarNumber) && p.CarNumber.Contains(keyword) 
                || !string.IsNullOrEmpty(p.DriverName) && p.DriverName.Contains(keyword) 
                || !string.IsNullOrEmpty(p.CarType) && p.CarType.Contains(keyword)));
            }
            var count = await data.CountAsync();
            var list = await data.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToListAsync();

            return (list, count);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="carConfiguration"></param>
        /// <returns></returns>
        public async Task<int> UpdateCarConfiguration(CarConfigurationDto carConfiguration)
        {
            var model = await dbContext.CarConfiguration.FirstOrDefaultAsync(p => p.SysID == carConfiguration.SysID);
            model.Merge(carConfiguration);
            return await dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="driverConfiguration"></param>
        /// <returns></returns>
        public async Task<int> AddDriverConfiguration(DriverConfigurationDto driverConfiguration)
        {
            driverConfiguration.SysID = Guid.NewGuid();
            await dbContext.DriverConfiguration.AddAsync(driverConfiguration);
            return await dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sysId"></param>
        /// <returns></returns>
        public async Task<int> DeleteDriverConfiguration(Guid sysId)
        {
            var model = await dbContext.DriverConfiguration.FirstOrDefaultAsync(p => p.SysID == sysId);
            dbContext.DriverConfiguration.Remove(model);
            return await dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public async Task<(List<DriverConfigurationDto> list, int count)> GetDriverConfigurationList(string userName, int PageIndex, int PageSize)
        {
            var data = dbContext.DriverConfiguration.Where(p => 1 == 1);
            if (!string.IsNullOrEmpty(userName))
            {
                data = data.Where(p => !string.IsNullOrEmpty(p.DriverName) && p.DriverName.Contains(userName));
            }
            var count = await data.CountAsync();
            var list = await data.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToListAsync();

            return (list, count);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public async Task<(List<DriverConfigurationDto> list, int count)> GetDriverListByKeyword(string keyword, int PageIndex, int PageSize)
        {
            var data = dbContext.DriverConfiguration.Where(p => 1 == 1);
            if (!string.IsNullOrEmpty(keyword))
            {
                data = data.Where(p => (!string.IsNullOrEmpty(p.DriverName) && p.DriverName.Contains(keyword)
                || !string.IsNullOrEmpty(p.DriverPhone) && p.DriverPhone.Contains(keyword)));
            }
            var count = await data.CountAsync();
            var list = await data.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToListAsync();

            return (list, count);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="driverConfiguration"></param>
        /// <returns></returns>
        public async Task<int> UpdateDriverConfiguration(DriverConfigurationDto driverConfiguration)
        {
            var model = await dbContext.DriverConfiguration.FirstOrDefaultAsync(p => p.SysID == driverConfiguration.SysID);
            model.Merge(driverConfiguration);
            return await dbContext.SaveChangesAsync();
        }
    }
}
